package com.shrct.businessprocess.product.impl;

import com.shrct.businessprocess.organization.RCEnterprise;
import com.shrct.businessprocess.product.RCIdentifiableNormalProductEntry;
import com.shrct.businessprocess.product.RCIdentifiableNormalProductEntryEvent;
import com.shrct.businessprocess.product.RCIdentifiableNormalProductEntryStatus;
import com.shrct.businessprocess.product.RCProductMetaType;
import com.shrct.businessprocess.product.impl.RCIdentifiableNormalProductEntryEventImpl;
import com.shrct.businessprocess.product.impl.RCIdentifiableProductEntryImpl;
import com.shrct.businessprocess.product.proxy.RCIdentifiableNormalProductEntryProxy;
import com.shrct.businessprocess.purchase.RCPurchaseOrderItem;
import edu.fudan.langlab.domain.statemachine.ModelObjectLogService;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCIDENTIFIABLENORMALPRODUCTENTRY_TYPE")
public class RCIdentifiableNormalProductEntryImpl extends RCIdentifiableProductEntryImpl implements RCIdentifiableNormalProductEntry {
  public RCIdentifiableNormalProductEntryImpl() {
    super();
  }
  
  private RCIdentifiableNormalProductEntryStatus Status;
  
  public RCIdentifiableNormalProductEntryStatus getStatus() {
    return this.Status;
  }
  
  private ModelObjectLogService getModelObjectLogService() {
    return getBeanFactory().getBean(ModelObjectLogService.class);
  }
  
  private void _start() {
    
    Status=RCIdentifiableNormalProductEntryStatus.Created;
  }
  
  public void start() {
    if(Status==null){
    	_start();
    	getModelObjectLogService().createLog(this,"Status","Start");
    	getBeanFactory().getBean(com.shrct.businessprocess.product.events.RCIdentifiableNormalProductEntryEventsManager.class).fireStarted(this);					
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException("allready started");
    }
  }
  
  private void __inStock(final RCPurchaseOrderItem orderItem) {
    
    Status=RCIdentifiableNormalProductEntryStatus.InStocking;
  }
  
  public void _inStock(final RCPurchaseOrderItem orderItem) {
    if(Status==RCIdentifiableNormalProductEntryStatus.Created){
    	__inStock(orderItem);
    	getModelObjectLogService().createLog(this,"Status","_inStock");
    	getBeanFactory().getBean(com.shrct.businessprocess.product.events.RCIdentifiableNormalProductEntryEventsManager.class).fire_inStock(this,orderItem);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void __deliver(final RCEnterprise enterprise) {
    
    Status=RCIdentifiableNormalProductEntryStatus.Delivered;
  }
  
  public void _deliver(final RCEnterprise enterprise) {
    if(Status==RCIdentifiableNormalProductEntryStatus.InStocking){
    	__deliver(enterprise);
    	getModelObjectLogService().createLog(this,"Status","_deliver");
    	getBeanFactory().getBean(com.shrct.businessprocess.product.events.RCIdentifiableNormalProductEntryEventsManager.class).fire_deliver(this,enterprise);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void __cancelInStock() {
    
  }
  
  public void _cancelInStock() {
    if(Status==RCIdentifiableNormalProductEntryStatus.InStocking){
    	__cancelInStock();
    	getModelObjectLogService().createLog(this,"Status","_cancelInStock");
    	getBeanFactory().getBean(com.shrct.businessprocess.product.events.RCIdentifiableNormalProductEntryEventsManager.class).fire_cancelInStock(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  private void __return() {
    
    Status=RCIdentifiableNormalProductEntryStatus.InStocking;
  }
  
  public void _return() {
    if(Status==RCIdentifiableNormalProductEntryStatus.Delivered){
    	__return();
    	getModelObjectLogService().createLog(this,"Status","_return");
    	getBeanFactory().getBean(com.shrct.businessprocess.product.events.RCIdentifiableNormalProductEntryEventsManager.class).fire_return(this);						
    }else{
    	throw new edu.fudan.langlab.domain.statemachine.InvalidStateException(Status.name());
    }
  }
  
  public Boolean isCreated() {
    return com.shrct.businessprocess.product.RCIdentifiableNormalProductEntryStatus.Created==Status;
  }
  
  public Boolean isInStocking() {
    return com.shrct.businessprocess.product.RCIdentifiableNormalProductEntryStatus.InStocking==Status;
  }
  
  public Boolean isDelivered() {
    return com.shrct.businessprocess.product.RCIdentifiableNormalProductEntryStatus.Delivered==Status;
  }
  
  @OneToMany(targetEntity = RCIdentifiableNormalProductEntryEventImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "product")
  private Collection<RCIdentifiableNormalProductEntryEvent> events = new java.util.ArrayList<com.shrct.businessprocess.product.RCIdentifiableNormalProductEntryEvent>();;
  
  public Collection<RCIdentifiableNormalProductEntryEvent> getEvents() {
    return this.events;
  }
  
  public RCIdentifiableNormalProductEntry removeAllEvents() {
    for(RCIdentifiableNormalProductEntryEvent obj : this.events){
    	getObjectFactory().delete(obj);
    }
    this.events.clear();
    return this;	
    
  }
  
  public RCIdentifiableNormalProductEntry removeFromEvents(final RCIdentifiableNormalProductEntryEvent rCIdentifiableNormalProductEntryEvent) {
    this.events.remove(rCIdentifiableNormalProductEntryEvent);
    getObjectFactory().delete(rCIdentifiableNormalProductEntryEvent);
    return this;
    
  }
  
  public RCProductMetaType getProductMetaType() {
    return RCProductMetaType.normalProduct;
  }
  
  public static RCIdentifiableNormalProductEntry create(final IObjectFactory objectFactory) {
    com.shrct.businessprocess.product.RCIdentifiableNormalProductEntry rCIdentifiableNormalProductEntry = new com.shrct.businessprocess.product.impl.RCIdentifiableNormalProductEntryImpl(
    );
    objectFactory.create(rCIdentifiableNormalProductEntry);
    return rCIdentifiableNormalProductEntry;			
    
  }
  
  public static RCIdentifiableNormalProductEntry createRCIdentifiableNormalProductEntryImpl(final IObjectFactory objectFactory, final Procedure1<RCIdentifiableNormalProductEntry> init) {
    com.shrct.businessprocess.product.RCIdentifiableNormalProductEntry rCIdentifiableNormalProductEntry = new com.shrct.businessprocess.product.impl.RCIdentifiableNormalProductEntryImpl(
    );
    objectFactory.create(rCIdentifiableNormalProductEntry);
    init.apply(rCIdentifiableNormalProductEntry);
    
    return rCIdentifiableNormalProductEntry;			
    
  }
  
  public RCIdentifiableNormalProductEntryProxy toProxy() {
    com.shrct.businessprocess.product.proxy.RCIdentifiableNormalProductEntryProxy proxy = new com.shrct.businessprocess.product.proxy.RCIdentifiableNormalProductEntryProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    proxy.setStatus(this.getStatus().name());
    return proxy;
    
  }
}
